package com.crealth;

import java.util.concurrent.CountDownLatch;

import org.apache.log4j.Logger;
import com.crealth.crmserver.CRMServer;
import com.crealth.crmserver.RequestMsgThread2;
import com.crealth.crmserver.ResponseMsgThread2;
import com.crealth.manager.LoanContractNoMgr;
import com.crealth.manager.MybatisManager;
import com.crealth.manager.SipAccountManager;
import com.crealth.memcached.MemcachedMgr;
import com.crealth.scheduler.QuartzScheduler;
import com.crealth.utils.Utils;

public class CrealthTestMain {
	private static Logger logger = Logger.getLogger("running_log"); //Logger.getLogger("running_log");
	private static Logger errorLog = Logger.getLogger("error_log");
	
	public static CountDownLatch threadSignal = new CountDownLatch(3);
	//private static ExecutorService requestExecutor = null;
	//private static ExecutorService responseExecutor = null;
	
	public static void main(String[] args) {
		try {
			String osName = Utils.getOSName();
			logger.info("OS: " + osName);			
			Config.initConfig();
			MybatisManager.initSessionFactory();
			MemcachedMgr.initMemcached();
		} catch (Exception e) {
			errorLog.error("init config error", e);
			System.exit(0);
		}
		
		try {
			QuartzScheduler.startScheduler();
		} catch (Exception e1) {
			errorLog.error("Quartz Scheduler start error", e1);
		}
		logger.info("----    QuartzScheduler finish    ----");
		
		ConstantDefine.initConstant();
		logger.info("----    ConstantDefine finish    ----");
		SipAccountManager.dispatchSipAccount();
		logger.info("----    Sip Account Distribute finish    ----");		
		LoanContractNoMgr.initContractNo();
		
		logger.info("start CRMServer");
		CRMServer crmSvr = new CRMServer();
		crmSvr.start();
		
		//requestExecutor = Executors.newFixedThreadPool(Config.requestHandlerThread);
		//RequestMsgQueue.setRequestExecutor(requestExecutor);
		//responseExecutor = Executors.newFixedThreadPool(Config.responseHandlerThread);
		//ResponseMsgQueue.setResponseExecutor(responseExecutor);
		
		for (int i = 0; i < Config.requestHandlerThread; ++i) {
			RequestMsgThread2 requestThread = new RequestMsgThread2();
			requestThread.start();
		}		
		for (int i = 0; i < Config.responseHandlerThread; ++i) {
			ResponseMsgThread2 responseThread = new ResponseMsgThread2();
			responseThread.start();
		}
		
		try {
			threadSignal.await();
		} catch (InterruptedException e) {
			errorLog.error("", e);
		}
		
	}
	
	/*
	public static List<String> historyDays = new ArrayList<String>();
	public static void main(String[] args) {
		try {
			Config.initConfig();
			MybatisManager.initSessionFactory();
		} catch (Exception e) {
			logger.error("init config error", e);
			System.exit(0);
		}
		
		getHistoryDays();
		for (String curDay : historyDays) {
			logger.info("----   curDay: " + curDay + "    ----");
			String tbName = "e_cdr_" + curDay;
			List<CdrModel> cdrls = CdrDb.selectAllCdr(tbName);
			if (cdrls != null && cdrls.size() > 0) {
				for (CdrModel cdr : cdrls) {
					CdrDb.insertCdrAllField(cdr.getUserID(),
							cdr.getCallee(), cdr.getHoldTm(),
							cdr.getEndreason(), cdr.getCallTime());
				}
			}
		}		
	}
	
	private static void getHistoryDays() {
        Calendar start = Calendar.getInstance();  
        start.set(2016, 3, 9);
        Long startTIme = start.getTimeInMillis();
        Calendar end = Calendar.getInstance();
        end.set(2016, 3, 7);
        Long endTime = end.getTimeInMillis();
        Long oneDay = 1000 * 60 * 60 * 24L;
        Long time = startTIme;
        while (time >= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyyMMdd");  
            historyDays.add(df.format(d));
            time -= oneDay;
        }
    }
	*/
	
}
